﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

namespace WebCurriculumn.Helper
{
    /// <summary>
    /// ステータス
    /// </summary>
    public enum LogStatus
    {
        OK = 0,
        NG = 1
    }
    public class LogInfo
    {
        /// <summary>
        /// ログ表示：日時
        /// </summary>
        public string DateTime { get; set; }
        /// <summary>
        /// ログ表示：操作
        /// </summary>
        public string LogAction { get; set; }
        /// <summary>
        /// ログ表示：操作詳細
        /// </summary>
        public string DetailAction { get; set; }
        /// <summary>
        /// ログ表示：操作者
        /// </summary>
        public string Operator { get; set; }
    }

    public class LogHelper
    {
        /// <summary>
        /// ログファイルの保存のフォルダ
        /// </summary>
        public static string LogFileFolder = ConfigurationManager.AppSettings["LogFileFolder"];
        /// <summary>
        /// ログの周期(MONTH, WEEK, DAY)
        /// </summary>
        public static string LogCycle = ConfigurationManager.AppSettings["LogCycle"].ToUpper();
        /// <summary>
        /// ログの形式
        /// </summary>
        public static string LogFormat = "日時:{0}, 操作:{1}, 操作詳細:{2},操作者:{3}";

        /// <summary>
        /// 基本的なログの出力のメッソド
        /// </summary>
        /// <param name="detailAction"></param>
        /// <param name="operatorID"></param>
        /// <param name="actionType"></param>
        /// <param name="point"></param>
        /// <param name="status"></param>
        /// <param name="reason"></param>
        public static void WriteLog(string logAction, string detailAction, string operatorID)
        {
            LogInfo loginfo = new LogInfo();

            loginfo.DateTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            loginfo.LogAction = logAction;
            loginfo.DetailAction = detailAction;
            loginfo.Operator = operatorID;
            WriteLog(loginfo);
        }

        /// <summary>
        /// 基本的なログの出力のメッソド
        /// </summary>
        /// <param name="loginfo"></param>
        public static void WriteLog(LogInfo loginfo)
        {
            string fileName = GetLogFileName();

            //日時:{0}, 操作:{1},  操作詳細:{2}, 操作者:{3}
            string logString = string.Format(LogHelper.LogFormat
                                             , loginfo.DateTime
                                             , loginfo.LogAction
                                             , loginfo.DetailAction
                                             , loginfo.Operator
                                             );

            System.Diagnostics.Debug.WriteLine("ログ:" + logString);
            if(!Directory.Exists(LogHelper.LogFileFolder))
            {
                Directory.CreateDirectory(LogHelper.LogFileFolder);
            }

            if (!File.Exists(LogHelper.LogFileFolder + fileName))
            {
                using (StreamWriter write = File.CreateText(LogHelper.LogFileFolder + fileName))
                {
                    write.WriteLine(logString);
                }
            }
            else
            {
                using (StreamWriter write = File.AppendText(LogHelper.LogFileFolder + fileName))
                {
                    write.WriteLine(logString);
                }
            }
        }

        /// <summary>
        /// デイバグのため,エラーが発生したら、ログを出す
        /// </summary>
        /// <param name="message"></param>
        public static void WriteDebug(string message)
        {
            string fileName = GetLogFileName();

            System.Diagnostics.Debug.WriteLine("エラー:" + message);
            using (StreamWriter write = File.AppendText(LogHelper.LogFileFolder + "Debug\\" + fileName))
            {
                write.WriteLine(message);
            }
        }
        /// <summary>
        /// ログファイルの名を取得する。
        /// </summary>
        /// <returns></returns>
        public static string GetLogFileName()
        {
            string fileName = "";

            if (LogCycle == "MONTH")
            {
                //fileName = DateTime.Today.ToString("yyyy-MM") + ".log";
                fileName = DateTime.Today.ToString("yyyyMM") + ".log";
            }
            else if (LogCycle == "WEEK")
            {
                DateTime monday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek);
            }
            else if (LogCycle == "DAY")
            {
                //fileName = DateTime.Today.ToString("yyyy-MM-dd") + ".log";
                fileName = DateTime.Today.ToString("yyyyMMdd") + ".log";
            }
            else
            {
                fileName = "WebCurriCulumnLog.log";
            }

            return fileName;
        }
    }
}